'use strict';

const path = require('path');
const fs = require('fs')
const appDirectory = fs.realpathSync(process.cwd());
const resolveApp = relativePath => path.resolve(appDirectory, relativePath);


// 解析src/pages路径下的所有文件夹
let appPagesConfig = require(resolveApp('src/pages/config'))
let configDirs = appPagesConfig.filter(item=>item.isBuild).map(item=>item.path)
console.log(configDirs)
const appPages = resolveApp('src/pages')
// let dirs = fs.readdirSync(resolveApp(appPages))
//   .filter(dir=>{
//     return fs.statSync(resolveApp(`${appPages}/${dir}`)).isDirectory()
//   })

// 生成打包入口的配置
const buildEntryConfig = (isEnvDevelopment)=>{
  return configDirs.reduce((obj,dir)=>{
    obj[dir]=[
      // isEnvDevelopment && require.resolve('react-dev-utils/webpackHotDevClient'),
      resolveApp(`${appPages}/${dir}/index.js`)
    ].filter(Boolean)
    return obj
  },{})
}
// 用于检测入口文件夹下面是否有index.js入口文件，便于yarn start和 yarn build运行时检测
const checkRequiredFiles = configDirs.map(dir=>{
  return resolveApp(`${appPages}/${dir}/index.js`)
})

// config after eject: we're in ./config/
module.exports = {
  appPublic: resolveApp('public'),
  appHtml: resolveApp('public/index.html'),
  appBuild: resolveApp('build'),
  appSrc: resolveApp('src'),
  appSrcIndex: resolveApp('src/index'),
  appPages : configDirs,
  resolveApp:resolveApp
}
module.exports.checkRequiredFiles = checkRequiredFiles
module.exports.buildEntryConfig = buildEntryConfig

